import operator
import os
import re
#读取文件，用字符串speech_text存储
speech_text = ''
f = open(os.path.abspath("Walden.txt"),encoding="utf-8")
speech_text = f.read()
f.close()
#将字符串全部转化为小写并分割
speech =speech_text.lower().split()
#统计词频
dict = {}
for word in speech:
    if re.findall("^[a-z].[a-z]$",word):
        if word not in dict:
            dict[word] = 1
        else:
            dict[word] += 1
#按词频从大到小排序
sort_dict = sorted(dict.items(),key=operator.itemgetter(1),reverse=True)
print(sort_dict)
